package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReplaceQues.aibkQuesReplaceZHT

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import android.widget.ImageView
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TestPaperReplaceQuesInfor
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import kotlinx.android.synthetic.main.aibk_adapter_item_child_xzt.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_childask.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*

/**
 * auther: 王国龙
 * Date：2020.05.22
 * describe:
 */
class QuesReplaceAdapterChildXzt(
        val activity: AppCompatActivity,
        val testPaperReplaceQuesInfor: TestPaperReplaceQuesInfor,
        val index_A:Int,
        val index_B:Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        var rootView:View? = null
        if(position == getChoiceTotalSize()+1){
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_itemanalysis, parent, false)
        }else if(position == 0){
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_childask, parent, false)
        }else {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_child_xzt, parent, false)
        }
        return rootView
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        if(position == getChoiceTotalSize()+1){
            val childAnalysis = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildAnalysis
            CustomTextViewToPicture(activity,holder.convertView.TvItemAnalysis, LancooMethod.getNewItemAnalysis(childAnalysis))
        }else if(position == 0){
            val itemSortIndex = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ItemList[0].ItemSortIndex
            val childAsk = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildAsk
            holder.convertView.TvChildAskPositionX.text = "$itemSortIndex. "
            CustomTextViewToPicture(activity,holder.convertView.TvChildAsk,childAsk)

            //只有当小题中含有音频题干时才显示小题音频播放器，其余时候隐藏播放器
            val path = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildAudio.Path
            if (path.isEmpty()) {
                holder.convertView.IvChildAudioPlayA.visibility = View.GONE
            }else{
                holder.convertView.IvChildAudioPlayA.visibility = View.VISIBLE
                childAudioListener!!.onInitChildAudioPlayer(path,holder.convertView.IvChildAudioPlayA)
            }

        }else{
            var optionIndex = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildOptionList[position-1].OptionIndex
            var option = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildOptionList[position-1].Option
            var showType = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildOptionList[position-1].ShowType
            if(showType == 0) {
                holder.convertView.BtnOptionIndex.text = optionIndex
                CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, option)
            }else{
                holder.convertView.BtnOptionIndex.text = option
                CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, "")
            }

            var itemAnswer = testPaperReplaceQuesInfor[index_A].ChildList[index_B].ItemList[0].ItemAnswer
            if (optionIndex == itemAnswer) {
                holder.convertView.BtnDuiGou.visibility = View.GONE
                holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.lancoooWhite))
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_green)
            } else {
                holder.convertView.BtnDuiGou.visibility = View.GONE
                holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.textColor))
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_white)
            }
        }

    }

    private fun getChoiceTotalSize():Int{
        return testPaperReplaceQuesInfor[index_A].ChildList[index_B].ChildOptionList.size
    }

    override fun getItemCount(): Int {
        return getChoiceTotalSize()+2
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    interface OnChildAudioListener {
        fun onInitChildAudioPlayer(path:String,ivPlay: ImageView)
    }

    private var childAudioListener: OnChildAudioListener? = null

    fun setChildAudioListener(childAudioListener: OnChildAudioListener?) {
        this.childAudioListener = childAudioListener
    }

}